package direct;

import com.rabbitmq.client.*;
import util.ConnectionUtil;

import java.io.IOException;

/**
 * @program: rabbitmq-demo1
 * @description: 消费者1
 * @author: luolidong
 * @create: 2021-06-27 19:29
 * @version: 1.0
 */
public class Consumer {
    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionUtil.createConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("myExchange2", "direct");
//        创建临时队列
        String queue = channel.queueDeclare().getQueue();
//        队列绑定交换机,和路由key,只获取info路由的信息
        channel.queueBind(queue, "myExchange2", "info");
//        获取消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1：" + new String(body));
            }
        });
    }
}
